package juc.deadlock;

import lombok.extern.slf4j.Slf4j;

import static juc.Sleeper.sleep;

@Slf4j(topic = "c.DeadLock01")
//展示什么是死锁现象
//t1线程获得A对象锁，接下来想获取B对象的锁
//t2线程获得B对象锁，接下来想获取A对象的锁
public class DeadLock01 {

    public static void main(String[] args) {
        //两个对象作为锁对象
        Object A = new Object();
        Object B = new Object();

        Thread t1 = new Thread(()->{
            synchronized (A){
                log.debug("lock A");
                sleep(1);
                synchronized (B){
                    log.debug("lock B");
                    log.debug("todo something");
                }
            }

        },"t1");

        Thread t2 = new Thread(()->{
            synchronized (B){
                log.debug("lock B");
                sleep(1);
                synchronized (A){
                    log.debug("lock A");
                    log.debug("todo something");
                }
            }

        },"t2");

        t1.start();
        t2.start();
    }
}
